package com.wangsd.web.sys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/sys/sysUser")
@Slf4j
public class SysUserController extends MyController {
    @Autowired
    private SysUserService service;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Object login(@ApiParam(value = "用户名", required = true) @RequestParam("username") String username,
                        @ApiParam(value = "密码", required = true) @RequestParam("password") String password) {
        SysUser user = service.selectByLogin(username, password);
        String token = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
        redisTemplate.opsForValue().set(token, user, 12, TimeUnit.HOURS);
        user.setToken(token);
        return Result.success(user);
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public Object logout() {
        String token = request.getHeader("Access-Token");
        if (token != null) {
            redisTemplate.delete(token);
        }
        return Result.success();
    }

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             SysUser queryObj) {
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = service.selectPage(queryObj, loginUser.getParentCode());
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "获取登录用户权限")
    @GetMapping("/selectPermission")
    public Object selectPermission() {
        SysUser loginUser = getUserInfo();
        loginUser = service.selectPermission(loginUser);
        return Result.success(loginUser);
    }

    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody SysUser entity) {
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "删除用户")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "用户id", required = true) @PathVariable Long id) {
        service.delete(id);
        return Result.success();
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@ApiParam(value = "旧密码", required = true) @RequestParam("oldPass") String oldPass,
                                  @ApiParam(value = "新密码", required = true) @RequestParam("password") String password) {
        SysUser loginUser = getUserInfo();
        service.updatePassword(oldPass, password, loginUser.getId());
        return Result.success();
    }
}
